$ ->

  # Create the namespace if it doesn't exist
  $.quesse = $.quesse || {}


  # A participant joined the hangout
  $.quesse.participantsAdded = (event) ->
    participants = $.quesse.parseParticipants(event)

    $.quesse.log('Participants joined the hangout.')

    for p in participants
      $.quesse.log(p.name + ' (' + p.id + ')')


  # A participant left the hangout
  $.quesse.participantsRemoved = (event) ->
    participants = $.quesse.parseParticipants(event)

    $.quesse.log('Participants left the hangout.')

    for p in participants
      $.quesse.log(p.name + ' (' + p.id + ')')


  # A participant joined or left the hangout
  $.quesse.participantsChanged = (event) ->
    $.quesse.log('Participants list changed.')

    participants = $.quesse.parseParticipants(event)

    for p in participants
      $.quesse.log(p.name + ' (' + p.id + ')')


  # A participant activated the extension
  $.quesse.participantsEnabled = (event) ->
    participants = $.quesse.parseParticipants(event)

    $.quesse.log('Participants activated the extension.')

    for p in participants
      $.quesse.log(p.name + ' (' + p.id + ')')


  # A participant desactivated the extension
  $.quesse.participantsDisabled = (event) ->
    participants = $.quesse.parseParticipants(event)

    $.quesse.log('Participants desactivated the extension.')

    for p in participants
      $.quesse.log(p.name + ' (' + p.id + ')')


  # The shared state changed
  $.quesse.sharedStateChanged = (event) ->
    $.quesse.log('The shared state changed.')
    console.log(event)


  $.quesse.updateSharedUser = (quesse_user_id, participant) ->
    # todo


  $.quesse.updateSharedOwner = (quesse_user_id) ->
    # todo


  $.quesse.updateSharedHangout = (hangout_infos) ->
    # todo


  # Parse and save the parameters when we first enter the hangout
  $.quesse.parseAndSaveParamsToSharedState = ->

    params = $.quesse.parseParameters()
    $.quesse.log('Parameters parsed.')

    if $.quesse.isANewcomer(params)
      $.quesse.log('First time.')
     $.quesse.log("parameter 'quesse_user_id': " + params['quesse_user_id'])
     $.quesse.log("parameter 'quesse_hangout_id': " + params['quesse_hangout_id'])

     $.quesse.updateSharedUser(params['quesse_user_id'], $.quesse.getMyself())

     if $.quesse.isTheHangoutOwner()
       $.quesse.updateSharedOwner(params['quesse_user_id'])
       $.quesse.updateSharedHangout($.quesse.getHangoutInfos())


  # Register callbacks
  $.quesse.registerCallbacks = ->
    gapi.hangout.onParticipantsAdded.add($.quesse.participantsAdded)
    $.quesse.log('Registered event: onParticipantsAdded.')

    gapi.hangout.onParticipantsRemoved.add($.quesse.participantsRemoved)
    $.quesse.log('Registered event: onParticipantsRemoved.')

    gapi.hangout.onParticipantsChanged.add($.quesse.participantsChanged)
    $.quesse.log('Registered event: onParticipantsChanged.')

    gapi.hangout.onParticipantsEnabled.add($.quesse.participantsEnabled)
    $.quesse.log('Registered event: onParticipantsEnabled.')

    gapi.hangout.onParticipantsDisabled.add($.quesse.participantsDisabled)
    $.quesse.log('Registered event: onParticipantsDisabled.')

    gapi.hangout.data.onStateChanged.add($.quesse.sharedStateChanged)
    $.quesse.log('Registered event: onStateChanged.')


  # Initialize the extension
  gapi.hangout.onApiReady.add (event) ->
    if event.isApiReady
      $.quesse.registerCallbacks()
      $.quesse.parseAndSaveParamsToSharedState()

      $.quesse.log('Google Hangout API initialized.')

